package com.StephenHu.Javabean;

import java.util.Random;

public class Role {

    //私有化成员属性
    private String name;
    private int blood;
    private int attack_power;
    private int defence_power;

    //空参构造方法
    public Role() {}

    //有参构造方法
    public Role(String name, int blood, int attack_power, int defence_power) {
        this.name = name;
        this.blood = blood;
        this.attack_power = attack_power;
        this.defence_power = defence_power;
    }

    //get & set
    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return blood
     */
    public int getBlood() {
        return blood;
    }

    /**
     * 设置
     * @param blood
     */
    public void setBlood(int blood) {
        this.blood = blood;
    }

    /**
     * 获取
     * @return attack_power
     */
    public int getAttack_power() {
        return attack_power;
    }

    /**
     * 设置
     * @param attack_power
     */
    public void setAttack_power(int attack_power) {
        this.attack_power = attack_power;
    }

    /**
     * 获取
     * @return defence_power
     */
    public int getDefence_power() {
        return defence_power;
    }

    /**
     * 设置
     * @param defence_power
     */
    public void setDefence_power(int defence_power) {
        this.defence_power = defence_power;
    }


    //角色方法
    public void attack(Role role){

        //随机数
        Random r = new Random();

        //计算攻击力区间(getAttack-5,getAttack+5)
        int hurt =(this.getAttack_power()-5)+r.nextInt(11);

        //计算防御力防御区间大小
        int offset = (role.getDefence_power()-5)+r.nextInt(11);
        if (offset > hurt){
            offset = hurt;
        }



        //计算造成的伤害
        int final_hurt = hurt - offset;


        //计算剩余血量
        int last_blood = role.getBlood() - final_hurt;
        if (last_blood < 0){
            last_blood = 0;
        }
        //修改挨揍的人的血量
        role.setBlood(last_blood);

        System.out.println(getName()+"攻击了"+role.getName()+",造成了"+hurt+"点伤害,但是"+role.getName()+
                "进行了防御,抵消了"+offset+"点伤害,"+"最终造成了"+final_hurt+"点伤害,"+role.getName()+
                "剩余血量"+last_blood);
    }
}
